<?php
namespace app\serveruser\service\auth;

use \app\manage\service\ApiBase;
/**
 * token的管理
 * @author adv30 <adv30@126.com> 2017年6月1日9:26:23
 */
class Token extends ApiBase
{
    protected $tokenValidate = '\app\serveruser\validate\Token';
    protected $tokenLogic = '\app\serveruser\logic\Token';
    // 根据传入的种子(不传也可)创造一个token
    public function create(): array
    {
        $data = $this->haveIssetReceiveData(['create_ip' => ' ', 'create_app_key'=>' ','other_info'=>'{}', 'roles'=>'']);
        return $this->stepRun($data, 'create', 'create');
    }
    // 根据token验证一条权限
    public function check(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ', 'auth'=> ' ']);
        return $this->stepRun($data, 'check', 'check');
    }
    // 根据token验证N条权限是否全部通过
    public function checkAuths(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ', 'auths'=> [' ']]);
        return $this->stepRun($data, 'checkAuths', 'checkAuths');
    }
    //根据token获取权限列表
    public function getAuthByToken(): array 
    {
        $data = $this->haveIssetReceiveData(['token'=>' ']);
        return $this->stepRun($data, 'getAuthByToken', 'getAuthByToken');
    }

    // 清理所有token
    public function clean(): array
    {
        return $this->logic($this->tokenLogic)->clean();
    }
    // 删除某一个token
    public function delete(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ']);
        return $this->stepRun($data, 'delete', 'delete');
    }
    // 根据token获取其保存的内容
    public function hGetAll(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ']);
        return $this->stepRun($data, 'hGetAll', 'hGetAll');
    }
    //手动给某个token延时“过期时间”
    public function continueLife(): array 
    {
        $data = $this->haveIssetReceiveData(['token'=>' ']);
        return $this->stepRun($data, 'continueLife', '');
    }
    // 在某个token保存信息
    public function hMset(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ', 'arr'=> [] ]);
        return $this->stepRun($data, 'hMset', 'hMset');
    }
    // 将token中保存的信息置空
    public function empty(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ']);
        return $this->stepRun($data, 'empty', 'empty');
    }
    // 是否设置了某一个变量
    public function hExists(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ', 'key' =>' ']);
        return $this->stepRun($data, 'hExists', 'hExists');
    }
    // 给一个token增加角色,并返回增加后的角色
    public function addRoles(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ', 'roles' =>' ']);
        return $this->stepRun($data, 'addRoles', 'addRoles');
    }
    // 给一个token减去角色,并返回余下的角色
    public function subRoles(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ', 'roles' =>' ']);
        return $this->stepRun($data, 'subRoles', 'addRoles');
    }
    // 取得当前token中保存的角色
    public function getRoles(): array
    {
        $data = $this->haveIssetReceiveData(['token'=>' ']);
        return $this->stepRun($data, 'getRoles', 'getRoles');
    }
}


